package com.whmnx.my.ui.viewmodel;

import android.Manifest;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.tencent.qcloud.ugckit.utils.ToastUtil;
import com.whmnx.base.bean.KefuBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.base.view.CommonDialog2;
import com.whmnx.my.R;
import com.whmnx.my.ui.fragment.MyKefuFragment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;

/**
 * author : huang
 * date   : 2020/9/18 0018 19:43
 * version: 1.0
 */
public class KefuModel extends ToolbarViewModel {

    public ObservableField<String> Value1 = new ObservableField<>("");
    public ObservableField<String> Value2 = new ObservableField<>("");
    public ObservableField<String> Value3 = new ObservableField<>("");

    MyKefuFragment mFragment;
    private String value,value2,value3;

    public KefuModel(@NonNull Application application) {
        super(application);
    }


    public void initToolbar(MyKefuFragment myKefuFragment) {
        this.mFragment = myKefuFragment;
        setTitleText("联系客服");
        initView();
    }

    private void initView() {
        Map<String, Object> params = new HashMap<>();
        HttpClient.get(mFragment.getActivity(), Constants.CUSTOMERSERVICE, params,new CallBack<List<KefuBean>>() {
            @Override
            public void onSuccess(List<KefuBean> result) {
                value = result.get(0).getValue();
                value2 = result.get(1).getValue();
                value3 = result.get(2).getValue();
                Value1.set(result.get(0).getValue());
                Value2.set(result.get(1).getValue());
                Value3.set(result.get(2).getValue());
            }
        });
    }

    public BindingCommand OnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {  //Balance
            String s2 = "tel:" + value;
            new CommonDialog2(mFragment.getActivity(), R.style.dialog, s2,"", (dialog, confirm) -> {
                if (confirm) {
                    //请求打开相机权限
                    RxPermissions rxPermissions = new RxPermissions(mFragment);
                    rxPermissions.request(Manifest.permission.CALL_PHONE)
                            .subscribe(new Consumer<Boolean>() {
                                @Override
                                public void accept(Boolean aBoolean) throws Exception {
                                    if (aBoolean) {
                                        Intent intent = new Intent(Intent.ACTION_DIAL);
                                        Uri data = Uri.parse("tel:" + value);
                                        intent.setData(data);
                                        mFragment.startActivity(intent);
                                    } else {

                                    }
                                }
                            });
                }
                dialog.dismiss();
            }).show();

        }
    });
    public BindingCommand OnClickCommand2 = new BindingCommand(new BindingAction() {
        @Override
        public void call() {  //Balance
            if (android.os.Build.VERSION.SDK_INT > 11) {
                android.content.ClipboardManager c = (android.content.ClipboardManager) mFragment.getActivity()
                        .getSystemService(Context.CLIPBOARD_SERVICE);
                c.setText(value2);
                ToastUtil.toastShortMessage("复制成功");

            } else {
                android.text.ClipboardManager c = (android.text.ClipboardManager) mFragment.getActivity()
                        .getSystemService(Context.CLIPBOARD_SERVICE);
                c.setText(value2);
                ToastUtil.toastShortMessage("复制成功");
            }

        }
    });
    public BindingCommand OnClickCommand3 = new BindingCommand(new BindingAction() {
        @Override
        public void call() {  //Balance
            if (android.os.Build.VERSION.SDK_INT > 11) {
                android.content.ClipboardManager c = (android.content.ClipboardManager) mFragment.getActivity()
                        .getSystemService(Context.CLIPBOARD_SERVICE);
                c.setText(value3);
                ToastUtil.toastShortMessage("复制成功");

            } else {
                android.text.ClipboardManager c = (android.text.ClipboardManager) mFragment.getActivity()
                        .getSystemService(Context.CLIPBOARD_SERVICE);
                c.setText(value3);
                ToastUtil.toastShortMessage("复制成功");
            }
        }
    });

}
